//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Spam
{
	/// <summary>
	/// Summary description for SpamModule.
	/// </summary>
	public class SpamModule : ICSModule
	{
		public SpamModule()
		{
		}

		public void Init(CSApplication csa, XmlNode node)
		{
			csa.PrePostUpdate += new CSPostEventHandler(csa_BeforeProcessPost);
		}

		private void csa_BeforeProcessPost(IContent content, CSPostEventArgs e)
		{
			try
			{
				// Check if post should be rated for spam
				Post post = content as Post;
				if ((post == null) || (e.State != ObjectState.Create))
					return;

				// Get spam configuration settings
				SpamConfiguration config = SpamConfiguration.Instance();
				bool audit = config.EnableSpamScoreAudit;
				int autoModerateScore = post.Section.SpamAutoModerateScore;
				if(autoModerateScore <= 0)
					autoModerateScore = config.AutoModerateScore;
				int autoDeleteScore = post.Section.SpamAutoDeleteScore;
				if(autoDeleteScore <= 0)
					autoDeleteScore = config.AutoDeleteScore;
				bool log = true;

				// Initialize spam runner
				SpamRunner runner = new SpamRunner(autoModerateScore, autoDeleteScore, audit, log);

				// Calculate score for post
				runner.RatePost(post, e);
			}
			catch(Exception ex)
			{
				EventLogs.Warn("Failed to finished processing spam rules", "Spam Rules", 444, ex, CSContext.Current.SettingsID);
			}
		}
	}
}
